<?php
// +----------------------------------------------------------------------
// | 功能说明"
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2030 http://www.jmzh1688.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: lxl
// | jmzhfsauto
// +----------------------------------------------------------------------
namespace app\admin\model;
use Think\Db;
use think\Model;

class WorkflowformextendModel extends Model
{
    
    protected $name = 'wf_form_extend';
    protected $autoWriteTimestamp = true;//时间自动更新 create_time ,update_time

   protected $field = true;
    public $title = '名称';






    public function edit($param)
    {
        try{




            $result =  $this->update($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                $this->edit_field($param['id']);
                return msg(1,url('formextend/index'), '修改文章成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }


    public function add($param)
    {
        try{
            $param['isdelete']=0;
            $result =  $this->save($param);
            if(false === $result){
                return msg(-1, '', $this->getError());
            }else{
                $this->add_field($this->id);
                return msg(1,url('formextend/index'), '添加文章成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    public function getById($id)
    {
        return $this->where('id', $id)->find();
    }

    public function delById($id)
    {
        try{
            $this->where('id', $id)->update(['isdelete'=>'1']);
            return msg(1, '', '删除成功');
        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

    public function getByWhere($where, $offset, $limit,$whereor='')
    {
        return $this->where($where)->where($whereor)->limit($offset, $limit)->order('id desc')->select();
    }

    public function getAllCount($where,$whereor='')
    {
        return $this->where($where)->where($whereor)->count();
    }

    /*$contentadd$*/
    public function add_field($id)
    {
        $table = Db::name('wf_form_extend')->find($id);
        $fields = $this->get_db_column_comment($table['code_name']);
        foreach($fields as $k=>$v){
            $fields[$k]['form_extend_id'] = $id;
        }
        Db::name('wf_form_field')->insertAll($fields);
    }
    public function edit_field($id)
    {
        $table = Db::name('wf_form_extend')->find($id);
        $fields = $this->get_db_column_comment($table['code_name']);
        foreach($fields as $k=>$v){
            $fields[$k]['form_extend_id'] = $id;
        }
        Db::name('wf_form_field')->where(['form_extend_id'=>$id])->delete();
        Db::name('wf_form_field')->insertAll($fields);
    }

    /**
     * 获取表字段和描述
     * @param string $table_name
     * @param bool $field
     * @param string $table_schema
     * @return mixed|string
     */
    function get_db_column_comment($table_name = '', $field = true, $table_schema = '')
    {
        // 接收参数
        $database = config('database');
        $table_schema = empty($table_schema) ? $database['database'] : $table_schema;
        $table_name = $database['prefix'] . $table_name;

        // 缓存名称
        $fieldName = $field === true ? 'allField' : $field;
        $cacheKeyName = 'db_' . $table_schema . '_' . $table_name . '_' . $fieldName;

        // 处理参数
        $param = [
            $table_name,
            $table_schema
        ];
        // 字段
        $columeName = '';
        if ($field !== true) {
            $param[] = $field;
            $columeName = "AND COLUMN_NAME = ?";
        }

        // 查询结果
        $result = Db:: query("SELECT COLUMN_NAME as field,column_comment as comment FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = ? AND table_schema = ? $columeName", $param);
        // pp(Db :: getlastsql());

        if (empty($result) && $field !== true) {
            return $table_name . '表' . $field . '字段不存在';
        }
        $data = array();

        foreach($result as $key=>$val){
            if(empty($v['comment'])){
                $data[$key]['field_name'] = $val['field'];
            }else{
                $data[$key]['field_name'] = $val['comment'];
            }
            $data[$key]['field_type'] = 1;
            $data[$key]['extend_field'] = $val['field'];
        }

//        return count($data) == 1 ? reset($data) : $data;
        return $data;
    }
}
